/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RTPEditor;

import java.io.IOException;
import javax.swing.JOptionPane;
import jpcap.JpcapCaptor;
import jpcap.JpcapWriter;
import jpcap.packet.Packet;
import jpcap.packet.UDPPacket;

/**
 *
 * @author Zarko Coklin
 */
public class SaveThread extends Thread
{

    public SaveThread(MainFrame mainFrame)
    {
        mf = mainFrame;
    }

    @Override
    public void run()
    {

        JpcapCaptor captor;
        JpcapWriter writer;
        Packet packet;

        int packetCnt = 0;
        int frequency = mf.getFrequency();
        int numDropped = mf.getNumDropped();

        try
        {
            captor = JpcapCaptor.openFile(mf.getInputFileName());
            writer = JpcapWriter.openDumpFile(captor, mf.getOutputFileName());
            packet = captor.getPacket();
            // Non Uniform Distribution
            if (mf.isUniform() == false)
            {
                packetCnt++;
                while (packet != null && packet != UDPPacket.EOF)
                {
                    if ((packetCnt % frequency) >= numDropped)
                    {
                        writer.writePacket(packet);
                    }
                    packet = captor.getPacket();
                    packetCnt++;
                }
            }
            else
            {
                // Close and reopen so packets can be read from the beginning
                captor.close();
                packetCnt = 0;
                captor = JpcapCaptor.openFile(mf.getInputFileName());
                packet = captor.getPacket();
                float increment = (float)100 / (100 - mf.getPercentage());
                float result = increment;
                while (packet != null && packet != UDPPacket.EOF)
                {
                    if ((float)packetCnt > result)
                    {
                        writer.writePacket(packet);
                        result += increment;
                    }
                    packet = captor.getPacket();
                    packetCnt++;
                }
            }
            captor.close();
            writer.close();
        }
        catch (IOException ex)
        {
            mf.getProgressScreen().dispose();
            JOptionPane.showMessageDialog(mf,
                    "Problem opening pcap file. " + System.getProperty("line.separator") +
                    "- Check the path" + System.getProperty("line.separator") +
                    "- Make sure it is a real pcap file" + System.getProperty("line.separator"),
                    "File Operation",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
        catch (UnsatisfiedLinkError exc)
        {
            mf.getProgressScreen().dispose();
            JOptionPane.showMessageDialog(mf,
                    "Problem opening pcap file. " + System.getProperty("line.separator") +
                    "- Make sure JPcap is installed: " + System.getProperty("line.separator") +
                    "(http://netresearch.ics.uci.edu/kfujii/jpcap/doc/)",
                    "File Operation",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
        mf.getProgressScreen().dispose();
        return;
    }

    private MainFrame mf;
}
